
//
//  PickProjectTableView.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/8/19.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//

import UIKit
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}


class PickProjectTableView: UITableView,UITableViewDelegate, UITableViewDataSource, DetailListCellDisplayViewProtcol {

    var realHeightDelegate: DetailListCellProtcol?
    
    var heightDelegate: DetailListCellProtcol?{
        set{
            realHeightDelegate = newValue
        }
        get{
            return realHeightDelegate
        }
    }
    var anyInfo:AnyObject?{
        didSet{
            let originString = anyInfo as? String
            var stringArray = originString?.components(separatedBy: "-")
            guard stringArray?.count > 0 else{
                return
            }
            stringArray!.remove(at: 0)
            var tempArray = [[String : String]]()
            for string in stringArray! {
                var array = string.components(separatedBy: " ")
                let dict = ["拣货规格":array[0].components(separatedBy: "：").last! , "拣货位":array[1].components(separatedBy: "：").last!]
                tempArray.append(dict)
            }
            dataArray = tempArray
        }
    }
    var dataArray:[[String : String]]?{
        didSet{
            self.reloadData()
            if self.delegate != nil{
                self.heightDelegate?.setRealHeight(44 * CGFloat((dataArray?.count)!))
            }
        }
    }

    
    
    //MARK: - life cycle
    override init(frame: CGRect, style: UITableViewStyle) {
        super.init(frame: frame, style: style)
        self.register(PickProjectTableViewCell.self, forCellReuseIdentifier: "PickProjectTableViewCell")
        self.delegate = self
        self.dataSource = self
        self.isScrollEnabled = false
        dataArray = [["拣货位" : "无数据"]]
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

  //MARK: - tableView dataSourece
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray?.count ?? 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var dict = self.dataArray![(indexPath as NSIndexPath).row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "PickProjectTableViewCell") as! PickProjectTableViewCell
        if dataArray?.first!["拣货位"] != "无数据" {
            dict.updateValue(String(format: "%0.2d" , (indexPath as NSIndexPath).row + 1) , forKey: "project")
        }
        cell.info = dict
        return cell
    }

}
